//
//  MVVMTestTabView.swift
//  CHSimpleUI
//
//  Created by cyptt on 2023/9/6.
//

import UIKit
import CHSimpleUI
import MJRefresh
class MVVMTestTabView: UIView,UITableViewDelegate  {

    
    var tabdataSource:MVVMTestDataSource?
    let cellID:String = CHUtils.getClassName(MVVMTestTabCell.self) + "ID"   // xib CELLID 为 类名加ID
    
    var viewModel:MVVMTestViewModel?  //ViewModel
    let reHeadr:MJRefreshNormalHeader = MJRefreshNormalHeader() //下拉刷新
    let footHeader:MJRefreshBackNormalFooter = MJRefreshBackNormalFooter() //上啦加载更多
    
    public weak var mController:UIViewController?
    public init(controller:UIViewController){
        super.init(frame: CGRect.zero)
        mController = controller
        config()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        config()
    

      
       
        
    }
    func config(){
        self.setUI()
        
        self.setPosition()
        
        self.setDataSourceViewModel()
        
//        self.viewModel?.refreshData(isMoreData: false)
        
        //  需要下拉刷新打开注释，注释 refreshData
        self.configRefresh()
        
        //需要上拉刷新打开注释，
        self.configPullUpRefresh()
    }
    
    func setUI(){
        self.addSubview(self.tableView)
    }
    
    func setPosition(){
        self.tableView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    func setDataSourceViewModel(){
        weak var weakSelf = self
        tabdataSource = MVVMTestDataSource.init(identifier: self.cellID) { cell, model, indexPath, tableView in
            //            let model:<#model#>? = model as? <#model#>
            //            let cell:<#TabCell#>? = cell as? <#TabCell#>
            //            let tableView:<#tableView#>? = tableView as? <#tableView#>
            //            let indexPath:<#IndexPath#>? = indexPath as? <#IndexPath#>
        }
        self.viewModel = MVVMTestViewModel.init(successBlock: { vModelResult, isMoreData in
            weakSelf?.tabdataSource?.datas = vModelResult as! [Any]
            
            if isMoreData == false{
                weakSelf?.endHeaderRefresh()
            }else{
                weakSelf?.endFooterRefresh()
            }
        
   
        },failBlock: { vModelFailMsg in
            
        })
        
        self.tableView.dataSource  = self.tabdataSource
    }

    
    /*
     初始化下拉刷新
     */
    func configRefresh(){
        reHeadr.setRefreshingTarget(self, refreshingAction: #selector(refreshData))
        self.tableView.mj_header = reHeadr
        self.tableView.mj_header?.beginRefreshing()
        
    }
    
    
   /*
    下拉刷新
    */
    @objc func refreshData(){
        self.viewModel?.refreshData(isMoreData: false,pageSize:10)
    }
    
    /*
     下拉停止了
     */
    func endHeaderRefresh(){
     
        self.tableView.mj_header?.endRefreshing()
        tabReload()
    }
    
    /*
     上拉加载更多
     */
    func configPullUpRefresh(){
        
        footHeader.setRefreshingTarget(self, refreshingAction: #selector(footReFreshData))
     
        self.tableView.mj_footer = footHeader
    
    }
    
    
   /*
    上拉加载更多
    */
    @objc func footReFreshData(){
        self.viewModel?.refreshData(isMoreData: true,pageSize:10)
    }
    
    /*
     上拉结束
     */
    func endFooterRefresh(){
       
        self.tableView.mj_footer?.endRefreshing()
        tabReload()
    }
    
    func tabReload(){
        self.tableView.reloadData()
        self.tableView.d_reloadData()
    }
    
 
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var tableView = {()-> UITableView in
        let tabV = UITableView.init();
        tabV.backgroundColor = UIColor.clear
        tabV.separatorColor = UIColor.clear
    
        tabV.delegate = self
        return tabV;
    }()
    
}


//MARK -- UITableViewDelegate
extension MVVMTestTabView{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 60
    }
}
//MARK -- CHTableViewPlaceHolderDelegate
extension MVVMTestTabView{
    
    func makePlaceHolderView() -> UIView? {
        
        //MARK: - 这里添加你的占位图
        let v:UIView = UIView.init()
        v.backgroundColor = UIColor.red
 
        return v
    }
    func enableScrollWhenPlaceHolderViewShowing() -> Bool {
        return true
    }
}

